package config;

import handler.JarLocation;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;

public class ConfigManager {
    private static final String CONFIG_FILE = "config.properties";
    private static final Properties properties;

    // 填充
    private static final String DB_TYPE;
    private static final String DB_IP;
    private static final String DB_PORT;
    private static final String DB_USER;
    private static final String DB_PASSWORD;
    private static final String PLC_ADDRESS;
    private static final String SLAVES_ID;
    private static final String FILE_ID;
    private static final String PLC_PORT;

    public static String getDbType() {
        return DB_TYPE;
    }

    public static String getDbIp() {
        return DB_IP;
    }

    public static String getDbPort() {
        return DB_PORT;
    }

    public static String getDbUser() {
        return DB_USER;
    }

    public static String getDbPassword() {
        return DB_PASSWORD;
    }

    public static String getPlcAddress() {
        return PLC_ADDRESS;
    }

    public static String getSlavesId() {
        return SLAVES_ID;
    }

    public static String getFileId() {
        return FILE_ID;
    }

    public static String getPLCPort() {
        return PLC_PORT;
    }

    static {
        properties = new Properties();
        try {
            InputStream inputStream = new FileInputStream(JarLocation.configPath + CONFIG_FILE);
            properties.load(inputStream);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 填充
        DB_TYPE = getProperty("DB_TYPE");
        DB_IP = getProperty("DB_IP");
        DB_PORT = getProperty("DB_PORT");
        DB_USER = getProperty("DB_USER");
        DB_PASSWORD = getProperty("DB_PASSWORD");
        PLC_ADDRESS = getProperty("PLC_ADDRESS");
        SLAVES_ID = getProperty("SLAVES_ID");
        FILE_ID = getProperty("FILE_ID");
        PLC_PORT = getProperty("PLC_PORT");
    }

    public static String getProperty(String key) {
        return properties.getProperty(key);
    }

    public static void setProperty(String key, String value) {
        properties.setProperty(key, value);
        saveProperties();
    }

    public static void setProperties(String key, String value) {
        try {
            // Save properties to file
            FileOutputStream outputStream = new FileOutputStream(JarLocation.confPath + "name.properties");
            Properties name = getProperties("name");
            name.setProperty(key, value);
            name.store(outputStream, null);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void saveProperties() {
        try {
            // 配置文件的路径
            String configFilePath = JarLocation.configPath + CONFIG_FILE;
            // 创建File对象
            File configFile = new File(configFilePath);
            // 检查目录是否存在
            if (!configFile.getParentFile().exists()) {
                if (!configFile.getParentFile().mkdirs()) {
                    System.out.println("创建目录失败: " + configFile.getParentFile().getAbsolutePath());
                    return; // 目录创建失败，返回
                }
            }
            // Save properties to file
            FileOutputStream outputStream = new FileOutputStream(configFilePath);

            properties.store(outputStream, null);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Properties getProperties(String name) {
        Properties properties = new Properties();
        try {
            // 配置文件的路径
            String configFilePath = JarLocation.confPath + name + ".properties";
            // 创建File对象
            File configFile = new File(configFilePath);
            // 检查目录是否存在
            if (!configFile.getParentFile().exists()) {
                if (!configFile.getParentFile().mkdirs()) {
                    System.out.println("创建目录失败: " + configFile.getParentFile().getAbsolutePath());
                    return null; // 目录创建失败，返回
                }
            }
            // 检查文件是否存在
            if (!configFile.exists()) {
                // 如果文件不存在，创建文件
                if (configFile.createNewFile()) {
                    System.out.println("创建了新文件: " + configFile.getAbsolutePath());
                } else {
                    System.out.println("创建文件失败: " + configFile.getAbsolutePath());
                    return null; // 创建文件失败，返回
                }
            }
            InputStream inputStream = Files.newInputStream(Paths.get(configFilePath));
            properties.load(inputStream);
            return properties;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

}
